package com.csd.serviceImpl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.csd.dao.ProjectDAO;
import com.csd.dao.StudentDAO;
import com.csd.dao.TeamDAO;
import com.csd.exception.*;
import com.csd.helper.IConstant;
import com.csd.helper.SessionManager;
import com.csd.helper.Tool;
import com.csd.pojo.Student;
import com.csd.pojo.Team;
import com.csd.service.ITeamService;
import com.csd.vo.UserVO;

/**
 * 
 * Service：团队
 * @author csd
 * @version 1.0
 * 
 * <br> 修订时间：2009-12-17
 * <br> 修改原因：创建文件
 * <br> 修改人：刘世彬
 * 
 * <br> 
 * 
 */
public class TeamService implements ITeamService {

	private TeamDAO teamDAO;
	private StudentDAO studentDAO;
	private ProjectDAO projectDAO;

	public ProjectDAO getProjectDAO() {
		return projectDAO;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}

	public StudentDAO getStudentDAO() {
		return studentDAO;
	}

	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	public TeamDAO getTeamDAO() {
		return teamDAO;
	}

	public void setTeamDAO(TeamDAO teamDAO) {
		this.teamDAO = teamDAO;
	}

	public Team getTeamByProjectID(Integer projectID) {
		Team team = null;
		team = teamDAO.findByProjectID(projectID);
		return team;
	}

	/**
	 * 登录
	 * @param UserVO
	 * @return UserVO
	 */
	public UserVO login(UserVO userVO) {
		UserVO uVO = null;
		try {
			Team temp = null;
			List<Team> list = null;
			//根据用户名查找用户，在匹配用户密码是否正确
			list = teamDAO.findByUsername(userVO.getLoginName());
			if (!list.isEmpty() && list.size() == 1) {
				temp = list.get(0);
				if (userVO.getPassword().equals(temp.getPassword())) {
					uVO = new UserVO(temp.getId(), temp.getUsername(),
							IConstant.teamRole, temp.getName());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("登录错误");
		}
		return uVO;
	}
	
	/**
	 * 根据ID删除团队 
	 * @param ID
	 * @return boolean
	 */
	public boolean deleteTeam(Integer ID) {
		boolean tag = false;

		try {
			Team team = teamDAO.findById(ID);
			teamDAO.delete(team);
			tag = true;
		} catch (RuntimeException re) {
			throw new DeleteException(re, "删除团队异常:参数ID=" + ID);
		}

		return tag;
	}
	
	/**
	 * 根据ID查找团队 
	 * @param ID
	 * @return Team
	 */
	public Team getTeamByID(Integer ID) {
		Team team = null;

		try {
			team = teamDAO.findById(ID);
		} catch (RuntimeException re) {
			throw new GetException(re, "查询团队异常:参数ID=" + ID);
		}

		return team;
	}
	
	/**
	 * 查询所有团队
	 * @param 
	 * @return List<Team>
	 */
	public List<Team> listTeam() {
		List<Team> list = null;

		try {
			list = teamDAO.findAll();
		} catch (RuntimeException re) {
			throw new GetException(re, "查询所有团队异常:参数:无");
		}

		return list;
	}

	/**
	 * 重新设置团队队长
	 * @param leaderID
	 * @return boolean
	 */
	public boolean resetLeader(Integer leaderID) {
		boolean tag = false;

		try {
			int id = SessionManager.getUserID();
			Team team = teamDAO.findById(id);
			Student leader = studentDAO.findById(leaderID);
			if (leader != null) {
				team.setLeader(leader);
				teamDAO.update(team);
				tag = true;
			}

		} catch (RuntimeException re) {
			throw new UpdateException(re, "重新设置队长异常:参数leaderID=" + leaderID);
		}

		return tag;
	}
	
	/**
	 * 重新设置密码
	 * @param oldPassword newPassword 
	 * @return boolean
	 */
	public boolean resetPassword(String oldPassword, String newPassword) {
		boolean tag = false;
		try {
			UserVO vo = SessionManager.getUserVO();
			if (vo != null && vo.getType() == IConstant.teamRole) {
				Team team = teamDAO.findById(vo.getUserID());
				if (team.getPassword().equals(oldPassword)) {
					team.setPassword(newPassword);
					teamDAO.update(team);
					tag = true;
				}
			}

		} catch (RuntimeException re) {
			throw new UpdateException(re, "重新设置密码异常:密码错误" + oldPassword);
		}
		return tag;
	}
	
	/**
	 * 添加一个团队 
	 * @param team
	 * @return boolean
	 */
	public boolean saveTeam(Team team) {
		boolean tag = false;

		try {
			team.setTime(Tool.getCurrentDate());
			teamDAO.save(team);
			tag = true;
		} catch (RuntimeException re) {
			throw new SaveException(re, "团队注册异常" + team);
		}

		return tag;
	}
	/**
	 * 更新团队
	 * @param team
	 * @return boolean
	 */
	@SuppressWarnings("unchecked")
	public boolean updateTeam(Team team) {
		boolean tag = false;

		try 
		{
			Integer id = SessionManager.getUserID();
			if (id != null) 
			{
				List list = projectDAO.findByTeam(id);
				if (list == null || list.size() == 0) 
				{
					teamDAO.update(team);
					UserVO vo = SessionManager.getUserVO();
					vo.setName(team.getName());
					HttpSession session = ServletActionContext.getRequest().getSession();
					session.setAttribute(IConstant.userVO, vo);
				}
				tag = true;
			}
		} 
		catch (RuntimeException re) 
		{
			throw new UpdateException(re, "更新团队异常" + team);
		}

		return tag;
	}
	
	/**
	 * 查询登录的团队
	 * @param 
	 * @return Team
	 */
	public Team getTeam() {
		Team team = null;

		try {
			int id = SessionManager.getUserID();
			team = teamDAO.findById(id);

		} catch (Exception re) {
			throw new GetException(re, "查看团队信息异常");
		}

		return team;
	}

	/**
	 * 检测用户名
	 * @param userName
	 * @return boolean
	 */
	public boolean checkUserName(String userName) {
		boolean isExist = false;
		try {
			List list = teamDAO.findByUsername(userName);
			if (list != null && list.size() > 0) {
				isExist = true;
			}
		} catch (Exception re) {
			re.printStackTrace();
		}

		return isExist;
	}

	public boolean hasProjectOrNot(Integer ID) {

		return teamDAO.hasProjectOrNot(ID);
	}
	public int getTeamMemberNumberByID(Integer ID){
		return teamDAO.getTeamMemberNumberByID(ID);
	}
}
